package com.panfeng.xcloud.boss.provider.member.security.verifyCode;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.panfeng.xcloud.common.core.enums.ResponseStatusEnum;
import com.panfeng.xcloud.common.core.web.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component("verifyCodeAuthenticationFailureHandler")
@Slf4j
public class VerifyCodeAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

	@Resource
	private ObjectMapper objectMapper;

	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
                                        AuthenticationException exception) throws IOException, ServletException {
		log.info(">>> verifyCodeAuthenticationFailureHandler ==>登录失败,异常信息：{}",exception.getMessage());
		response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
		response.setContentType("application/json;charset=UTF-8");
		ResponseVO responseVO = new ResponseVO();
		responseVO.setCode(ResponseStatusEnum.LOGIN_AUTHENTICATION_FAILURE.getCode());
		responseVO.setMessage(exception.getMessage());
		response.getWriter().write(objectMapper.writeValueAsString(responseVO));
	}

}
